package com.mnurre.shopping.assistant;

import com.mnurre.shopping.assistant.R;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;

/**
 * @author michael
 *
 */
public class ManageCategoriesActivity extends ListActivity {

	private static final String TAG = "ManageCategoriesActivity";
	
	private CategoriesData mData;
	private Cursor mCursor;
	
	private static final int CATEGORY_ADD = 0;
	private static final int CATEGORY_EDIT = 1;
	// private static final int CATEGORY_DELETE = 2;
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		Log.d(TAG, "Creating activity.");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_manage_categories);
		Log.d(TAG, "Populating the categories.");
		populateCategories();
		Log.d(TAG, "Registering for context menu with the list view.");
		registerForContextMenu(getListView());
	}
	

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo)
	 */
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		Log.d(TAG, "Creating context menu.");
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_manage_context, menu);
		Log.d(TAG, "Done creating context menu.");
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		Log.d(TAG, "Creating options menu.");
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_manage, menu);
		Log.d(TAG, "Done creating options menu.");
		return true;
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onContextItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		Log.d(TAG, "Content menu item selected.");
		AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
		switch(item.getItemId()){
		case R.id.manage_menu_context_option_delete:
			Log.d(TAG, "Delete context menu item selected.");
			mData.open();
			mData.deleteCategory(info.id);
			mData.close();
			populateCategories();
			break;
		case R.id.manage_menu_context_option_edit:
			Log.d(TAG, "Edit context menu item selected.");
			// open the edit category screen
			Intent intent = new Intent(this, EditCategoryActivity.class);
			intent.putExtra(DataManager.Tables.Categories.COLUMN_ID, info.id);
			startActivityForResult(intent, CATEGORY_EDIT);
			Log.d(TAG, "Done launching activity to edit category.");
			break;
		}
		Log.d(TAG, "Return super.onContextItemSelected(item)");
		return super.onContextItemSelected(item);
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onMenuItemSelected(int, android.view.MenuItem)
	 */
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		Log.d(TAG, "Handling the menu item selected.");
		switch(item.getItemId()){
			case R.id.menu_manage_add:
				Log.d(TAG, "The Add menu item was selected. Starting intent to add a new category.");
				Intent intent = new Intent(this, EditCategoryActivity.class);
				Log.d(TAG, "Starting EditCategoryActivity.");
				startActivityForResult(intent, CATEGORY_ADD);
				break;
		}
		return super.onMenuItemSelected(featureId, item);
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		populateCategories();
	}

	private void populateCategories(){
		Log.d(TAG, "populateCategories called.");
		Log.d(TAG, "Creating a new instance of CategoriesData.");
		mData = new CategoriesData(this);
		Log.d(TAG, "Opening data.");
		mData.open();
		Log.d(TAG, "Getting a cursor for all categories.");
		mCursor = mData.getCategories();
		Log.d(TAG, "Calling startManagingCursor.");
		startManagingCursor(mCursor);
		Log.d(TAG, "Creating from string array.");
		String[] from = new String[] { DataManager.Tables.Categories.COLUMN_NAME, 
				DataManager.Tables.Categories.COLUMN_IS_TAXABLE };
		Log.d(TAG, "Creating to int array.");
		int[] to = new int[] { R.id.categoryItemNameTextView, R.id.categoryItemIsTaxableTextView };
		Log.d(TAG, "Creating a new CategoriesCursorAdapter.");
		CategoriesCursorAdapter adapter = new CategoriesCursorAdapter(this, R.layout.category_item, mCursor, from, to);
		Log.d(TAG, "Calling setListAdapter for custom cursor adapter.");
		setListAdapter(adapter);
//		Log.d(TAG, "Calling close on data.");
//		mData.close();
		Log.d(TAG, "Done in populateCategories.");
	}
	
}
